package com.example.commom.es;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

public class documentOperation implements ElasticsearchOperation {

    protected documentOperation() {

    }

    /**
     *  发送同步更新/修改 图书的 es 请求
     */
    @Override
    public boolean executeUpdate(RestHighLevelClient client, IndexRequest request) {
        try {
            IndexResponse response = client.index( request, RequestOptions.DEFAULT);
            return response.getResult() == IndexResponse.Result.CREATED
                    || response.getResult() == IndexResponse.Result.UPDATED;
        } catch (IOException e) {
            return false;
        }
    }

    /**
     *  发送同步删除图书信息的 es 请求
     */
    @Override
    public boolean executeDelete(RestHighLevelClient client, DeleteRequest request) {
        try {
            DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);

            return response.getResult() == DeleteResponse.Result.DELETED ;
        } catch (IOException e) {
            return false;
        }
    }
}
